﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;

namespace DAO
{
    public class MEMBERDAO
    {
        public static bool TonTai_Email(String emailNew)
        {
            DataClassesNHOMMUADataContext db = new DataClassesNHOMMUADataContext();
            var temp = db.usp_CheckEmail(emailNew);
            if (temp.Count() > 0)
            {
                return true;
            }
            return false;
        }

        public static bool TonTai_UserName(String userNew)
        {
            DataClassesNHOMMUADataContext db = new DataClassesNHOMMUADataContext();
            var temp = db.usp_CheckUSERNAME(userNew);
            if (temp.Count() > 0)
            {
                return true;
            }
            return false;
        }

        public static MEMBER LayMemberCoID(int idMember)
        {
            DataClassesNHOMMUADataContext db = new DataClassesNHOMMUADataContext();
            var temp = db.usp_SelectMemberByID(idMember);
            return (temp.ToArray<MEMBER>())[0]; 
        }

        public static MEMBER LayMemberCoEmail(String email)
        {
            DataClassesNHOMMUADataContext db = new DataClassesNHOMMUADataContext();
            var temp = db.usp_SelectMemberByEmail(email);
            return (temp.ToArray<MEMBER>())[0];
        }

        public static MEMBER LayMemberCoUser(String user)
        {
            DataClassesNHOMMUADataContext db = new DataClassesNHOMMUADataContext();
            var temp = db.usp_SelectMemberByUserName(user);
            return (temp.ToArray<MEMBER>())[0];
        }

        public static int ThemThanhVien(MEMBER memNew)
        {            
            DataClassesNHOMMUADataContext db = new DataClassesNHOMMUADataContext();
            int res = db.usp_InsertMember(memNew.USERNAME,memNew.PASSWORD,
                memNew.NAME, memNew.GENDER, memNew.BIRTHDAY, memNew.PHONE, memNew.ADDRESS,
                memNew.EMAIL,memNew.CMND); 
            return res;
        }

        //tuy

        public static MEMBER[] LayBang()
        {
            DataClassesNHOMMUADataContext db = new DataClassesNHOMMUADataContext();
            var products = from v in db.MEMBERs
                           select v;
            //db.usp_SelectProductsAll();            
            return products.ToArray<MEMBER>();
        }

        public static void AddMember(MEMBER mb)
        {
            DataClassesNHOMMUADataContext db = new DataClassesNHOMMUADataContext();
            db.MEMBERs.InsertOnSubmit(mb);
            db.SubmitChanges();
        }

        //public static MEMBER LayMemberCoID(int idMember)
        //{
        //    DataClassesNHOMMUADataContext db = new DataClassesNHOMMUADataContext();
        //    var temp = from v in db.MEMBERs
        //               where v.IDMEMBER == idMember
        //               select v;
        //    return (temp.ToArray<MEMBER>())[0];
        //}

        public static void updateMember(int IDMember, string password, string hoten, string diachi, string dienthoai)
        {
            DataClassesNHOMMUADataContext db = new DataClassesNHOMMUADataContext();
            var temp = (from v in db.MEMBERs
                        where v.IDMEMBER == IDMember
                        select v).Single();
            temp.PASSWORD = password;
            temp.PHONE = dienthoai;
            temp.ADDRESS = diachi;
            temp.NAME = hoten;
            
            db.SubmitChanges();
        }

        public static void deleteMember(int id)
        {
            DataClassesNHOMMUADataContext db = new DataClassesNHOMMUADataContext();
            MEMBER mb = db.MEMBERs.Single(m => m.IDMEMBER == id);
            db.MEMBERs.DeleteOnSubmit(mb);
            db.SubmitChanges();
        }
        public static MEMBER getMemberByEmail(string email)
        {
            DataClassesNHOMMUADataContext db = new DataClassesNHOMMUADataContext();
            var v = from q in db.MEMBERs
                    where q.EMAIL == email
                    select q;
            return (v.ToArray<MEMBER>())[0];
        }
        
        public static int getIDByUserName(string user)
        {
            DataClassesNHOMMUADataContext db = new DataClassesNHOMMUADataContext();
            var v = from q in db.MEMBERs
                    where q.USERNAME == user
                    select q.IDMEMBER;
            return (v.ToArray<int>())[0];
        }
    }
}
